package com.tanhua.server.controller;

import com.tanhua.dubbo.server.vo.CommentType;
import com.tanhua.dubbo.server.vo.IdType;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private QuanZiService quanZiService;


    /**
     * 控制层实现功能:查找小视频列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> getAllVideo(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = null;
        try {
            pageResult = videoService.findAllVideo(page, pageSize);
            if (pageResult == null) {
                return ResponseEntity.ok(new PageResult());
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(pageResult);

    }


    /**
     * 控制层实现功能:发布小视频
     */
    @PostMapping
    public ResponseEntity<Void> saveVideo(
            @RequestParam("videoThumbnail") MultipartFile picUrl,
            @RequestParam("videoFile") MultipartFile vicUrl) {
        try {
            Boolean flag = videoService.saveVideo(picUrl, vicUrl);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.status(501).build();
    }


    /**
     * 控制层实现功能:点赞小视频
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeVideo(@PathVariable("id") String videoId) {
        Long likeCount = null;
        try {
            likeCount = videoService.likeVideo(videoId);
            if (likeCount == null) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 控制层实现功能:取消点赞小视频
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeVideo(@PathVariable("id") String videoId) {
        Long likeCount = null;
        try {
            likeCount = videoService.disLikeVideo(videoId);
            if (likeCount == null) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(likeCount);
    }


    /**
     * 控制层实现功能:发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComment(
            @RequestBody Map<String, String> param,
            @PathVariable("id") String videoId) {
        try {
            String comment = param.get("comment");
            Boolean flag = videoService.saveComment(videoId, comment);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.status(501).build();
    }


    /**
     * 控制层实现功能:查看小视频的评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> getAllComment(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize, @PathVariable("id") String videoId) {
        PageResult pageResult = null;
        try {
            pageResult = videoService.getAllComment(videoId, page, pageSize);
            if (pageResult == null) {
                return ResponseEntity.ok(new PageResult());
            }
        } catch (Exception e) {
            log.error("控制层:====>查看小视频异常!");
            return ResponseEntity.ok(new PageResult());
        }
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 控制层实现功能:对评论进行点赞
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> likeCommentVideo(@PathVariable("id") String videoId) {
        Long result;
        try {
            result = quanZiService.likeAndLove(videoId, CommentType.LIKE, IdType.COMMENT);
            if (result == null) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //对小视频的评论进行点赞异常
            log.error("对小视频的评论进行点赞异常!");
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(null);
    }


    /**
     * 控制层实现功能:对评论取消点赞
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disLikeCommentVideo(@PathVariable("id") String videoId) {
        Long result;
        try {
            result = quanZiService.likeCancelAndLove(videoId, CommentType.LIKE, IdType.VIDEO);
            if (result == null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            //对小视频的评论进行点赞异常
            log.error("对小视频的评论进行点赞异常!");
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 控制层实现功能:小视频取消关注
     * @param userId
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity<Void> userUnFocus(@PathVariable("uid") Long userId) {
        Boolean result = videoService.userUnFocus(userId);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 控制层实现功能:对小视频进行关注
     *
     * @param userId
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity<Void> userFocus(@PathVariable("uid") Long userId) {
        Boolean result = videoService.userFocus(userId);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(null);
    }
}
